
public class CommandParser {
	
	// Returns array of [Equipment] [Command], [IP], [Port], Parsed from input string
	public String[] ParseInputFromSocket(String SocketInput)
	{
		String[] ParsedString = new String[5];
		ParsedString[0] = GetEquipmentType(SocketInput); 
		ParsedString[1] = GetCommandFromInput(SocketInput);
		ParsedString[2] = GetIPFromInput(SocketInput);
		ParsedString[3] = GetPortFromInput(SocketInput);
		ParsedString[4] = GetIDRateProfile(SocketInput); // for command SetPortRateProfile
		return ParsedString;
	}
	
	private String GetEquipmentType(String SocketInput) {
		int start = 0;
		int end = 4;
		char[] buffer = new char[end - start];
		SocketInput.getChars(start, end, buffer, 0);
		String Equipment = new String(buffer);
		return Equipment.trim();
	}


// Developing Version --------------------------------------------
	private String GetIPFromInput(String SocketInput)
	{
			int start = SocketInput.indexOf("Line: ")+5;
			int end = SocketInput.lastIndexOf(":0");
			char[] buffer = new char[end - start];
			SocketInput.getChars(start, end, buffer, 0);
			String IP = new String(buffer);
			return IP.trim();
	}
	
	private String GetPortFromInput(String SocketInput)
	{
		String Port;
		if (SocketInput.indexOf("IDRateProfile") == -1)
		{
			int start = SocketInput.indexOf(":0/")+1;
			int end = SocketInput.length();
			char[] buffer = new char[end - start];
			SocketInput.getChars(start, end, buffer, 0);
			Port = new String(buffer);
			return Port.trim();
		}
		else {
			int start = SocketInput.indexOf(":0/")+1;
			int end = SocketInput.indexOf("IDRateProfile")-1;
			char[] buffer = new char[end - start];
			SocketInput.getChars(start, end, buffer, 0);
			Port = new String(buffer);
			return Port.trim();
		}
	}
	
	
/*/ Working version ----------------------------------------------
	private String GetIPFromInput(String SocketInput)
	{
		if (SocketInput.indexOf("IDRateProfile") == -1)
		{
			int start = SocketInput.indexOf("Line: ")+5;
			int end = SocketInput.lastIndexOf(":0");
			char[] buffer = new char[end - start];
			SocketInput.getChars(start, end, buffer, 0);
			String IP = new String(buffer);
			return IP.trim();
		}
		else {
			int start = SocketInput.lastIndexOf('\n')+6;
			int end = SocketInput.lastIndexOf(':');
			char[] buffer = new char[end - start];
			SocketInput.getChars(start, end, buffer, 0);
			String IP = new String(buffer);
			return IP.trim();
		}
	}

	private String GetPortFromInput(String SocketInput)
	{
		String Port;
		if (SocketInput.indexOf("IDRateProfile") == -1)
		{
			int start = SocketInput.lastIndexOf(':')+1;
			int end = SocketInput.length();
			char[] buffer = new char[end - start];
			SocketInput.getChars(start, end, buffer, 0);
			Port = new String(buffer);
			return Port.trim();
		}
		else {
			int start = SocketInput.indexOf('/')-1;
			int end = SocketInput.lastIndexOf('\n');
			char[] buffer = new char[end - start];
			SocketInput.getChars(start, end, buffer, 0);
			Port = new String(buffer);
			return Port.trim();
			
		}
	}

//-----------------------------------------------------------------/*/
	
	private String GetCommandFromInput(String SocketInput)
	{
		int start = SocketInput.indexOf(':')+2;
		int end = SocketInput.indexOf("Line")-2;
		char[] buffer = new char[end - start];
		SocketInput.getChars(start, end, buffer, 0);
		String Command = new String(buffer);
		return Command.trim();
	}
	
	private String GetIDRateProfile(String SocketInput)
	{
		if (SocketInput.indexOf("IDRateProfile") != -1)
		{
			int start = SocketInput.lastIndexOf(':')+2;
			int end = SocketInput.length();
			char[] buffer = new char[end - start];
			SocketInput.getChars(start, end, buffer, 0);
			String IRRateProfile = new String(buffer);
			return IRRateProfile.trim();
		}
		else return "";
	}
}
